package com.soft.mainsite.module.staticstrategy.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.soft.mainsite.module.staticstrategy.dao.StaticTriggerDao;
import com.soft.mainsite.module.staticstrategy.dao.TemplateManageDao;
import com.soft.mainsite.module.staticstrategy.entity.StaticStrategy;
import com.soft.mainsite.module.staticstrategy.entity.TemplateManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * StaticTriggerDao.java 此类用于模板静态化触发Service
 *
 * @author:刘兴家
 * @date:2019/4/24 0011
 * @remark:
 */
@Service
public class StaticTriggerServiceImpl implements StaticTriggerService {

	@Autowired
	private StaticTriggerDao staticTriggerDao;

	@Autowired
	private TemplateManageDao templateManageDao;


	/**
	 * 保存或修改
	 * @param staticStrategy
	 */
	@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW, rollbackFor=Exception.class)
	@Override
	public void saveOrUpdateTemplateTrigger(StaticStrategy staticStrategy) {

		List<String> fileNameList = staticStrategy.getStaticFileName();

		if(ObjectUtil.isNull(staticStrategy.getTemplateId())){
			return;
		}

		if (CollectionUtil.isNotEmpty(fileNameList) && fileNameList.size() > 0){
			fileNameList.removeAll(Collections.singleton(null));
		}


		staticTriggerDao.removeStaticTrigger(staticStrategy.getTemplateId());

		if (CollectionUtil.isEmpty(fileNameList)) {
			return;
		}
		fileNameList.forEach(item->{
			TemplateManage templateManage = templateManageDao.querfyTemplateManage(null, item);
			if (templateManage == null) {
				return;
			}
			staticTriggerDao.addStaticTrigger(staticStrategy.getTemplateId(), templateManage.getTemplateId(), 0);
		});
	}


}
